package java44th.group.service.admin.address;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketAddress;
import java44th.group.bean.po.MarketAddressExample;
import java44th.group.mapper.MarketAddressMapper;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ProjectName project-second
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/12 17:06
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public CommonData<MarketAddress> queryAddress(BasePageInfo basePageInfo, Integer userId, String name) {

        // 开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        // 设置排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        // 插入不为空的条件
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        // 查找所有符合条件的数据
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);

        // 将数据放入 分页 中
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(marketAddresses);

        return CommonData.data(marketAddressPageInfo);
    }
}
